package com.exam.controller;

import com.exam.aop.MyLog;
import com.exam.dto.ActionResult;
import com.exam.po.FillQuestion;
import com.exam.po.JudgeQuestion;
import com.exam.po.MultiQuestion;
import com.exam.service.AnswerService;
import com.exam.utils.ApiResultHandler;
import com.exam.vo.AnswerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhanggaoxiang
 * @ClassName AnswerController
 * @Description 题库
 * @Date 2021/1/12 10:46
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api")
public class AnswerController {
    @Autowired
    private AnswerService answerService;
    /**
     * @Author zhanggaoxiang
     * @Description 管理员分页查询题库
     * @Date 2021/1/12 10:58
     * @Param [page, size]
     * @return com.exam.dto.ActionResult
     **/
    @GetMapping("/findanswer/{page}/{size}")
    @ResponseBody
    @MyLog(name = "find answer")
    public ActionResult findAnswerByPage(@PathVariable Integer page,@PathVariable Integer size){
        Map<String,Object> map=new HashMap<>();
        Integer total=answerService.findAnswerCount();
        List<AnswerVO> list = answerService.findAnswerByPage(page,size);
        map.put("total",total);
        map.put("data",list);
       if (map!=null){
           return ApiResultHandler.buildActionResult(200,"查询成功!!",map);
       }else {
           return ApiResultHandler.buildActionResult(404,"查询失败!!",null);
       }
    }
    /**
     * @Author zhanggaoxiang
     * @Description 根据id删除题库
     * @Date 2021/1/12 13:17
     * @Param [questionId]
     * @return com.exam.dto.ActionResult
     **/
    @DeleteMapping("/deleteanswer/{questionId}")
    @ResponseBody
    @MyLog(name = "delete answer")
    public ActionResult deleteAnswerById(@PathVariable Integer questionId){
        FillQuestion fillQuestion  = answerService.findFillQuesstionById(questionId);
        if (fillQuestion!=null){
            int i=answerService.deleteFillQuesstionById(questionId);
            if (i>0){
                return ApiResultHandler.buildActionResult(200,"删除成功!!",null);
            }else {
                return ApiResultHandler.buildActionResult(403,"删除失败!!",null);
            }
        }
        JudgeQuestion judgeQuestion= answerService.findJudegeQuesstionById(questionId);
        if (judgeQuestion!=null){
            int i=answerService.deleteJudegeQuesstionById(questionId);
            if (i>0){
                return ApiResultHandler.buildActionResult(200,"删除成功!!",null);
            }else {
                return ApiResultHandler.buildActionResult(403,"删除失败!!",null);
            }
        }
        MultiQuestion multiQuestion= answerService.findMultiQuesstionById(questionId);
        if (multiQuestion!=null){
            int i=answerService.deleteMultiQuesstionById(questionId);
            if (i>0){
                return ApiResultHandler.buildActionResult(200,"删除成功!!",null);
            }else {
                return ApiResultHandler.buildActionResult(403,"删除失败!!",null);
            }
        }
        return ApiResultHandler.buildActionResult(403,"删除失败!!",null);
    }


}
